package com.dcits.common.util;

import java.util.Hashtable;

import net.sf.json.JSONObject;

public class StringEx {
	public static final int indexOf(StringBuffer source, int start,
			String tobelocated) {
		return source.toString().indexOf(tobelocated, start);
	}

	public static String replace(String str, String oldStr, String newStr) {
		StringBuffer resStr;
		if ((null == str) || (null == oldStr) || (newStr == null))
			return str;

		if (str.length() > 1000000)
			;
		try {
			throw new Exception("警告：替换的字符串超过1M,可能造成JVM抖动!!");
		} catch (Exception ex) {
			ex.printStackTrace();

			int len = oldStr.length();
			String post = str;
			resStr = new StringBuffer("");
			int idx;
			while ((idx = post.indexOf(oldStr)) != -1) {
				String pre = post.substring(0, idx);
				post = post.substring(idx + len);

				resStr.append(pre).append(newStr);
			}
			resStr.append(post);
		}
		return resStr.toString();
	}

	public int occurence(String str, String s) {
		if ((null == str) || (null == s))
			return 0;
		int p = str.indexOf(s);
		int cnt = 0;
		if (p < 0)
			return cnt;
		int len = s.length();
		++cnt;
		while (p < str.length()) {
			p += len;
			p = str.indexOf(s, p);
			if (p < 0)
				return cnt;
			++cnt;
		}
		return (cnt++);
	}

	public static String sNull(Object obj) {
		return ((obj == null) ? "" : obj.toString());
	}
	public static String sNull(JSONObject obj,String key) {
		return ((obj.containsKey(key)) ? obj.getString(key) :"" );
	}
	public static String sNull(Object obj, boolean isconvert) {
		if (isconvert) {
			return ((obj == null) ? "" : obj.toString());
		}

		return ((obj == null) ? null : obj.toString());
	}

	public static String convGbk(String arg) throws Exception {
		if (arg == null)
			return arg;
		String s = null;

		byte[] b = arg.getBytes("ISO-8859-1");
		s = new String(b, "GBK");
		return s;
	}

	public static String reverseGbk(String arg) throws Exception {
		if (arg == null)
			return null;
		String s = null;

		byte[] b = arg.getBytes("GBK");
		s = new String(b, "ISO-8859-1");
		return s;
	}

	public static String convertCharSet(String arg, String source, String target)
			throws Exception {
		if ((null == arg) || (null == source) || (null == target))
			return arg;

		String s = null;

		byte[] b = arg.getBytes(source);
		s = new String(b, target);
		return s;
	}

	public static String replMark(String s) {
		if (null == s)
			return null;
		return s.replace('<', '[').replace('>', ']');
	}

	public static String getParm(Hashtable inputParms, String key) {
		if ((null == key) || (null == inputParms))
			return null;
		Object o1 = inputParms.get(key);
		if (o1 instanceof String)
			return ((String) inputParms.get(key));

		if (o1 instanceof String[]) {
			String[] s1 = (String[]) o1;
			if (s1 != null)
				return s1[0];
		}
		return null;
	}

	public static void main(String[] arg) {
		String s = "aaa&bbb&cccpp";
		System.out.println(replace(s, "&", "1&1;"));
		System.out.println(replace(s, "&", "1"));
		System.out.println(replace(s, "a", ""));
	}
}
